<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	class PresenterCreateNews {
		
		public function __construct() {}
		
		public function clickCreateNews ($news) {
			$command = CommandFactory :: createCommandCreateNews ($news['newsDate'],$news['imagePath'],$news['title'],$news['description']);
			
			$command -> execute();		
			return $command->getResult();
		
		}
		
		/**
		 * Method to execute to select news
		 */
		public function selectNews() {
			$command = CommandFactory :: createCommandSelectNews();
			$command -> execute();
			$news = $command -> getResult();
			
			$result = '<table border="0" cellpadding="5" cellspacing="5">';
			$i = 0;
			while ($i < count($news['date'])) {
				$result .= '<tr>';
				$result .= '<td rowspan="2"><img src="../../images/logo.png" width="400" height="160" /></td>';
				$result .= '<td align="center"><b>' . $news['title'][$i] . ' (' . $news['date'][$i] . ')</b></td>';
				$result .= '</tr>';
				
				$result .= '<tr>';
				$result .= '<td align="left">';
				
				if (strlen($news['description'][$i]) > 100) {
					$result .= substr($news['description'][$i], 0, 100) . '...<br />';
					$result .= '<a href="#" onclick="xajax_showNews(\'' . $news['title'][$i] . '\', \'' . $news['date'][$i] . '\', \'' . $news['image'][$i] . '\', \'' . $news['description'][$i] . '\')">Ver art&iacute;culo completo</a>';
				}
				
				else
					$result .= $news['description'][$i];
				
				$result .= '</td>';
				$result .= '</tr>';
				$i++;
			}
			$result .= '</table>';
			return $result;
		}
		
		public function showNews($title, $date, $image, $description) {
			$result = '<table>';
			$result .= '<tr>';
			$result .= '<td align="left">';
			$result .= '<b>' . $title . '</b>';
			$result .= '&nbsp;(' . $date . ').&nbsp;';
			$result .= $description;
			$result .= '</td>';
			$result .= '</tr>';
			$result .= '<tr>';
			$result .= '<td align="center">';
			$result .= '<img src="../../images/logo.jpg" width="600" height="200" />';
			$result .= '</td>';
			$result .= '</tr>';
			$result .= '<tr>';
			$result .= '<td align="center"><a href="#" onclick="xajax_selectNews()">Regresar</a></td>';
			$result .= '</tr>';
			$result .= '</table>';
			
			return $result;
		}
	}
?>